package com.example.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.demo.mapper.WorkTypeMapper;
import com.example.demo.pojo.Worktype;
import com.example.demo.service.WorkTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class WorkTypeImpl implements WorkTypeService {
    @Resource
    WorkTypeMapper workTypeMapper;
    @Override
    public List<Worktype> worktypeselect(String id, String name) {
        List<Worktype> worktypes=workTypeMapper.worktypeselect(Integer.parseInt(id),name);
        return worktypes;
    }

    @Override
    public PageInfo<Worktype> worktypeinfo(int page, int size, String id, String name) {
        int c=0;
        if (id.equals("")){
            c=-1;
        }else{
            c=Integer.parseInt(id);
        }
        PageHelper.startPage(page,size);
        List<Worktype> worktypes=workTypeMapper.worktypeselect(c,name);
        PageInfo<Worktype> worktypePageInfo =new PageInfo<>(worktypes);
        return worktypePageInfo;
    }

    @Override
    public String updatework(String name, String seq) {
        String message="";
        List<Worktype> worktypes=workTypeMapper.worktypeselect(-1,name);
        if(worktypes.size()>=1){
            message="账户存在";
        }else{
            workTypeMapper.updatework(name,Integer.parseInt(seq));
            message="成功";
        }
       return JSON.toJSONString(message);
    }

    @Override
    public String workinsert(String worker) {
        String message="";
        List<Worktype> worktypes=workTypeMapper.worktypeselect(-1,worker);
        if(worktypes.size()>=1){
            message="账户存在";
        }else {
            workTypeMapper.workinsert(worker);
        }
       return JSON.toJSONString(message);
    }

    @Override
    public String workdelete(String id) {
        workTypeMapper.workdelete(Integer.parseInt(id));
        String message="成功";
        return JSON.toJSONString(message);
    }
}
